﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using WISD.DBUtility.DBOperate;

namespace WISD.CarFriend.BLL.OA
{
    public class ComplaintModule
    {
        #region MaticCode
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string ComplaintID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_CF_OA_Complaint");
            strSql.Append(" where ComplaintID=@ComplaintID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ComplaintID", SqlDbType.NVarChar,50)			};
            parameters[0].Value = ComplaintID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(WISD.CarFriend.Model.CFModel.T_CF_OA_Complaint model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CF_OA_Complaint(");
            strSql.Append("ComplaintID,ConsumeID,ReturnVisitType,StartTime,EndTime,Complaint,Result,Satisfaction,Memo,State,SendEmployee,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8)");
            strSql.Append(" values (");
            strSql.Append("@ComplaintID,@ConsumeID,@ReturnVisitType,@StartTime,@EndTime,@Complaint,@Result,@Satisfaction,@Memo,@State,@SendEmployee,@Reviser,@ReviseDate,@Memo1,@Memo2,@Memo3,@Memo4,@Memo5,@Memo6,@Memo7,@Memo8)");
            SqlParameter[] parameters = {
					new SqlParameter("@ComplaintID", SqlDbType.NVarChar,50),
					new SqlParameter("@ConsumeID", SqlDbType.Int,4),
					new SqlParameter("@ReturnVisitType", SqlDbType.Int,4),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@Complaint", SqlDbType.NVarChar,1000),
					new SqlParameter("@Result", SqlDbType.NChar,1000),
					new SqlParameter("@Satisfaction", SqlDbType.Int,4),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@SendEmployee", SqlDbType.NVarChar,100),
					new SqlParameter("@Reviser", SqlDbType.NVarChar,50),
					new SqlParameter("@ReviseDate", SqlDbType.DateTime),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.ComplaintID;
            parameters[1].Value = model.ConsumeID;
            parameters[2].Value = model.ReturnVisitType;
            parameters[3].Value = model.StartTime;
            parameters[4].Value = model.EndTime;
            parameters[5].Value = model.Complaint;
            parameters[6].Value = model.Result;
            parameters[7].Value = model.Satisfaction;
            parameters[8].Value = model.Memo;
            parameters[9].Value = model.State;
            parameters[10].Value = model.SendEmployee;
            parameters[11].Value = model.Reviser;
            parameters[12].Value = model.ReviseDate;
            parameters[13].Value = model.Memo1;
            parameters[14].Value = model.Memo2;
            parameters[15].Value = model.Memo3;
            parameters[16].Value = model.Memo4;
            parameters[17].Value = model.Memo5;
            parameters[18].Value = model.Memo6;
            parameters[19].Value = model.Memo7;
            parameters[20].Value = model.Memo8;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(WISD.CarFriend.Model.CFModel.T_CF_OA_Complaint model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CF_OA_Complaint set ");
            strSql.Append("ConsumeID=@ConsumeID,");
            strSql.Append("ReturnVisitType=@ReturnVisitType,");
            strSql.Append("StartTime=@StartTime,");
            strSql.Append("EndTime=@EndTime,");
            strSql.Append("Complaint=@Complaint,");
            strSql.Append("Result=@Result,");
            strSql.Append("Satisfaction=@Satisfaction,");
            strSql.Append("Memo=@Memo,");
            strSql.Append("State=@State,");
            strSql.Append("SendEmployee=@SendEmployee,");
            strSql.Append("Reviser=@Reviser,");
            strSql.Append("ReviseDate=@ReviseDate,");
            strSql.Append("Memo1=@Memo1,");
            strSql.Append("Memo2=@Memo2,");
            strSql.Append("Memo3=@Memo3,");
            strSql.Append("Memo4=@Memo4,");
            strSql.Append("Memo5=@Memo5,");
            strSql.Append("Memo6=@Memo6,");
            strSql.Append("Memo7=@Memo7,");
            strSql.Append("Memo8=@Memo8");
            strSql.Append(" where ComplaintID=@ComplaintID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ConsumeID", SqlDbType.Int,4),
					new SqlParameter("@ReturnVisitType", SqlDbType.Int,4),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@Complaint", SqlDbType.NVarChar,1000),
					new SqlParameter("@Result", SqlDbType.NChar,1000),
					new SqlParameter("@Satisfaction", SqlDbType.Int,4),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@SendEmployee", SqlDbType.NVarChar,100),
					new SqlParameter("@Reviser", SqlDbType.NVarChar,50),
					new SqlParameter("@ReviseDate", SqlDbType.DateTime),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100),
					new SqlParameter("@ComplaintID", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.ConsumeID;
            parameters[1].Value = model.ReturnVisitType;
            parameters[2].Value = model.StartTime;
            parameters[3].Value = model.EndTime;
            parameters[4].Value = model.Complaint;
            parameters[5].Value = model.Result;
            parameters[6].Value = model.Satisfaction;
            parameters[7].Value = model.Memo;
            parameters[8].Value = model.State;
            parameters[9].Value = model.SendEmployee;
            parameters[10].Value = model.Reviser;
            parameters[11].Value = model.ReviseDate;
            parameters[12].Value = model.Memo1;
            parameters[13].Value = model.Memo2;
            parameters[14].Value = model.Memo3;
            parameters[15].Value = model.Memo4;
            parameters[16].Value = model.Memo5;
            parameters[17].Value = model.Memo6;
            parameters[18].Value = model.Memo7;
            parameters[19].Value = model.Memo8;
            parameters[20].Value = model.ComplaintID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ComplaintID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CF_OA_Complaint ");
            strSql.Append(" where ComplaintID=@ComplaintID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ComplaintID", SqlDbType.NVarChar,50)			};
            parameters[0].Value = ComplaintID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ComplaintIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CF_OA_Complaint ");
            strSql.Append(" where ComplaintID in (" + ComplaintIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public WISD.CarFriend.Model.CFModel.T_CF_OA_Complaint GetModel(string ComplaintID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ComplaintID,ConsumeID,ReturnVisitType,StartTime,EndTime,Complaint,Result,Satisfaction,Memo,State,SendEmployee,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 from T_CF_OA_Complaint ");
            strSql.Append(" where ComplaintID=@ComplaintID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ComplaintID", SqlDbType.NVarChar,50)			};
            parameters[0].Value = ComplaintID;

            WISD.CarFriend.Model.CFModel.T_CF_OA_Complaint model = new WISD.CarFriend.Model.CFModel.T_CF_OA_Complaint();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ComplaintID"] != null && ds.Tables[0].Rows[0]["ComplaintID"].ToString() != "")
                {
                    model.ComplaintID = ds.Tables[0].Rows[0]["ComplaintID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ConsumeID"] != null && ds.Tables[0].Rows[0]["ConsumeID"].ToString() != "")
                {
                    model.ConsumeID = int.Parse(ds.Tables[0].Rows[0]["ConsumeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ReturnVisitType"] != null && ds.Tables[0].Rows[0]["ReturnVisitType"].ToString() != "")
                {
                    model.ReturnVisitType = int.Parse(ds.Tables[0].Rows[0]["ReturnVisitType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StartTime"] != null && ds.Tables[0].Rows[0]["StartTime"].ToString() != "")
                {
                    model.StartTime = DateTime.Parse(ds.Tables[0].Rows[0]["StartTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EndTime"] != null && ds.Tables[0].Rows[0]["EndTime"].ToString() != "")
                {
                    model.EndTime = DateTime.Parse(ds.Tables[0].Rows[0]["EndTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Complaint"] != null && ds.Tables[0].Rows[0]["Complaint"].ToString() != "")
                {
                    model.Complaint = ds.Tables[0].Rows[0]["Complaint"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Result"] != null && ds.Tables[0].Rows[0]["Result"].ToString() != "")
                {
                    model.Result = ds.Tables[0].Rows[0]["Result"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Satisfaction"] != null && ds.Tables[0].Rows[0]["Satisfaction"].ToString() != "")
                {
                    model.Satisfaction = int.Parse(ds.Tables[0].Rows[0]["Satisfaction"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Memo"] != null && ds.Tables[0].Rows[0]["Memo"].ToString() != "")
                {
                    model.Memo = ds.Tables[0].Rows[0]["Memo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["State"] != null && ds.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["State"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SendEmployee"] != null && ds.Tables[0].Rows[0]["SendEmployee"].ToString() != "")
                {
                    model.SendEmployee = ds.Tables[0].Rows[0]["SendEmployee"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Reviser"] != null && ds.Tables[0].Rows[0]["Reviser"].ToString() != "")
                {
                    model.Reviser = ds.Tables[0].Rows[0]["Reviser"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ReviseDate"] != null && ds.Tables[0].Rows[0]["ReviseDate"].ToString() != "")
                {
                    model.ReviseDate = DateTime.Parse(ds.Tables[0].Rows[0]["ReviseDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Memo1"] != null && ds.Tables[0].Rows[0]["Memo1"].ToString() != "")
                {
                    model.Memo1 = ds.Tables[0].Rows[0]["Memo1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo2"] != null && ds.Tables[0].Rows[0]["Memo2"].ToString() != "")
                {
                    model.Memo2 = ds.Tables[0].Rows[0]["Memo2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo3"] != null && ds.Tables[0].Rows[0]["Memo3"].ToString() != "")
                {
                    model.Memo3 = ds.Tables[0].Rows[0]["Memo3"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo4"] != null && ds.Tables[0].Rows[0]["Memo4"].ToString() != "")
                {
                    model.Memo4 = ds.Tables[0].Rows[0]["Memo4"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo5"] != null && ds.Tables[0].Rows[0]["Memo5"].ToString() != "")
                {
                    model.Memo5 = ds.Tables[0].Rows[0]["Memo5"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo6"] != null && ds.Tables[0].Rows[0]["Memo6"].ToString() != "")
                {
                    model.Memo6 = ds.Tables[0].Rows[0]["Memo6"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo7"] != null && ds.Tables[0].Rows[0]["Memo7"].ToString() != "")
                {
                    model.Memo7 = ds.Tables[0].Rows[0]["Memo7"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo8"] != null && ds.Tables[0].Rows[0]["Memo8"].ToString() != "")
                {
                    model.Memo8 = ds.Tables[0].Rows[0]["Memo8"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ComplaintID,ConsumeID,ReturnVisitType,StartTime,EndTime,Complaint,Result,Satisfaction,Memo,State,SendEmployee,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 ");
            strSql.Append(" FROM T_CF_OA_Complaint ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ComplaintID,ConsumeID,ReturnVisitType,StartTime,EndTime,Complaint,Result,Satisfaction,Memo,State,SendEmployee,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 ");
            strSql.Append(" FROM T_CF_OA_Complaint ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_CF_OA_Complaint ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ComplaintID desc");
            }
            strSql.Append(")AS Row, T.*  from T_CF_OA_Complaint T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion


        #region SelfCode
        
        #endregion
    }
}
